package ro.gillardon.beans;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.apache.log4j.Logger;

import ro.gillardon.business.exception.BusinessException;
import ro.gillardon.business.priceservice.TrainPriceService;
import ro.gillardon.business.priceservice.impl.train.TrainPriceServiceEJB;
import ro.gillardon.commons.Offers;
import ro.gillardon.commons.dto.TrainUI;
import ro.gillardon.commons.enums.City;
import ro.gillardon.commons.logger.BTPLogger;
import ro.gillardon.commons.mot.train.TrainClass;
import ro.gillardon.commons.mot.train.TrainConditions;
import ro.gillardon.commons.mot.train.TrainOfferDetails;
import ro.gillardon.commons.util.DateUtils;

@ManagedBean(name = "trainView", eager = true)
@ViewScoped
public class TrainView {
  private List<String> allLocations;
  private List<String> fromList;
  private List<String> toList;
  private Date date;
  private List<TrainUI> resultList;
  private String fromInput;
  private String toInput;

  private static final Logger LOG = Logger.getLogger(TrainView.class);

  @EJB
  private final TrainPriceService trainService =
    (TrainPriceService) BeanProvider.getEJB(TrainPriceServiceEJB.class.getSimpleName());

  public TrainView() {
    BTPLogger.info(LOG, TrainView.class.getSimpleName() + " initialized with service : " + this.trainService);
  }

  @PostConstruct
  public void init() {
    this.allLocations = new ArrayList<String>();
    this.allLocations.add("Bretten");
    this.allLocations.add("Cluj");
    this.allLocations.add("Munchen");

    this.fromList = new ArrayList<String>(this.allLocations);
    this.toList = new ArrayList<String>(this.allLocations);

    this.resultList = new ArrayList<TrainUI>();
  }

  public Date getDate() {
    return DateUtils.createNewDate(this.date);
  }

  public void setDate(final Date date) {
    this.date = DateUtils.createNewDate(date);
  }

  public List<TrainUI> getResultList() {
    return this.resultList;
  }

  public void setResultList(final List<TrainUI> resultList) {
    this.resultList = resultList;
  }

  public void setFromList(final List<String> from) {
    this.fromList = from;
  }

  public void setToList(final List<String> to) {
    this.toList = to;
  }

  public List<String> getFromList() {
    return this.fromList;
  }

  public List<String> getToList() {
    return this.toList;
  }

  public String getFromInput() {
    return this.fromInput;
  }

  public void setFromInput(final String fromInput) {
    this.fromInput = fromInput;
  }

  public String getToInput() {
    return this.toInput;
  }

  public void setToInput(final String toInput) {
    this.toInput = toInput;
  }

  public void onFromChange() {
    if (this.fromInput != null && !"".equals(this.fromInput)) {
      this.toList = new ArrayList<String>(this.allLocations);
      this.toList.remove(this.fromInput);

    }
  }

  public void onToChange() {
    if (this.toInput != null && !"".equals(this.toInput)) {
      this.date = new Date();

    }
  }

  //Under construction
  public void updateTable() {

    TrainConditions conditions =
      new TrainConditions(City.BRETTEN, City.CLUJ, DateUtils.convertToLocalDate(this.date), TrainClass.FIRST_CLASS);
    try {
      Offers<TrainOfferDetails> list = this.trainService.getOffers(conditions);
      System.out.println("Offers nr:" + list.getOfferDetails().size());
    } catch (BusinessException e) {
      BTPLogger.error(LOG, " " + e);
    }

    System.out.println("submit!!");
    System.out.println("from:" + this.fromInput);
    System.out.println("to:" + this.toInput);
    System.out.println("date:" + this.date);

    //please provide implementation
  }
}
